<?php
// include "pwd_session.php";
if (!empty($_POST['webname'])) {
	echo "<hr>";
	echo "****************************************--开始--******************************************";
	echo "<hr>";
}
$row = $dir = $html = $php = $js = $css = $png = $jpg = $txt = $other = 0;

//修改文件
if($_POST['create']=='create'){
	$handle = fopen($_POST['filename'], "w+");
	$w=fwrite($handle, $_POST['str']);
	if($w){
		echo "修改成功！";
		var_dump($w);
	}else{
		echo "修改失败！";
	}
	exit();
}
//获取文件内容
function getcon($filename){
	$handle = fopen($filename, "a+");
	$content=fread($handle, filesize($filename));
?>
	<form action="#" method="post">
		<input type="hidden" name="create" value="create" />
		文件名：<input name="filename" type="text" value="<?php echo $filename; ?>" style="width: 80%;"/>
		<p>内容：</p>
		<textarea style="width: 100%; height: 500px;" name="str" rows="10" cols="50"><?php echo $content; ?></textarea>
		<input type="submit" name="submit" value="提 交"   />
		<input type="reset" value="重 置" name="reset"  />
	</form>
<?php
	exit();	
}
//传入文件名
if($_POST['getcon']=='getcon'||$_GET['getcon']=='getcon'){
	$gfname=$_POST['gfname']?$_POST['gfname']:$_GET['gfname'];
	getcon($gfname);
	exit();
}

//读取所有
function getdir($path){
	global $dir,$html,$php,$js,$css,$png,$jpg,$txt,$other;
	$arr=scandir($path);
	foreach($arr as $v){
		if($v!="."&&$v!=".."){
			if(is_dir($path."/".$v)){
				$dir++;
				echo "目录：".$path."/".$v."<span style='color: sandybrown;'>==》》文件夹</span>"."<a style='border: 3px solid orange;' target='_blank' href='ftp.php?dirname=".$path . "/" . $v."'>进入文件夹</a>";
				echo "<br>";
//				getdir($path."/".$v);
			}else{
//				echo "文件：".$path."/".$v;
				$ext = substr($v, (strrpos($v, ".") + 1));
				if ($ext == 'html') {
					$html++;
					echo "文件：" . $path . "/" . $v."<span style='color: greenyellow;'>==》》HTML文件</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} elseif ($ext == 'php') {
					$php++;
					echo "文件：" . $path . "/" . $v . "<span style='color: blue;'>==》》PHP文件</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} elseif ($ext == 'js') {
					$js++;
					echo "文件：" . $path . "/" . $v . "<span style='color: blueviolet;'>==》》JS文件</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} elseif ($ext == 'css') {
					$css++;
					echo "文件：" . $path . "/" . $v . "<span style='color: coral;'>==》》css文件</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} elseif ($ext == 'png') {
					$png++;
					echo "文件：" . $path . "/" . $v . "<span style='color: #00BFFF;'>==》》png图片</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} elseif ($ext == 'jpg') { 
					$jpg++;
					echo "文件：" . $path . "/" . $v . "<span style='color: #0066B3;'>==》》JPG图片</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} elseif ($ext == 'txt') {
					$txt++;
					echo "文件：" . $path . "/" . $v . "<span style='color: lightslategrey;'>==》》TXT文件</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				} else {
					$other++;
					echo "文件：" . $path . "/" . $v . "<span style='color: red;'>==》》其他文件</span>"."<a style='border: 2px solid red;' target='_blank' href='ftp.php?getcon=getcon&gfname=".$path . "/" . $v."'>打开文件</a>";
				}
				echo "<br>";
			}
		}
	}		
}
$dirname = $_POST['webname']?$_POST['webname']:$_GET['dirname'];
if (!empty($dirname)) {
	echo $dirname;
	echo "<hr>";
	getdir($dirname);

	echo "<hr><hr>";
	echo "递归".$dirname . "</br>";
	echo "本次递归统计：递归文件夹" . $dir . "个！" ;
	echo "<br>";
	echo "遍历的文件总计:" . ($html+$php+$js+$css+$png+$jpg+$txt+$other) . "个!";
	echo "<br>";
	echo "其中：";
	echo "<br>";
	echo "遍历的HTML文件" . $html . "个!";
	echo "<br>";
	echo "遍历的PHP文件" . $php . "个!";
	echo "<br>";
	echo "遍历JS文件" . $js . "个!";
	echo "<br>";
	echo "遍历CSS文件" . $css . "个!";
	echo "<br>";
	echo "遍历png图片文件" . $png . "个!";
	echo "<br>";
	echo "遍历jpg图片文件" . $jpg . "个!";
	echo "<br>";
	echo "遍历txt文件" . $txt . "个!";
	echo "<br>";
	echo "遍历的其他文件" . $other . "个！<br>";
	echo "<hr>";
}
if (!empty($_POST['webname'])) {
	echo "<hr>";
	echo "****************************************--结束--******************************************";
	echo "<hr>";
}
?>
<html>
	<head>
		<meta charset="utf-8">
		<title><?php echo $dirname;?></title>
	</head>
	<body>
		<form action="#" method="post">
			<br />
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入要递归的文件夹：
			<input type="text" name="webname" id="webname" value="<?php echo $dirname;?>" /> <br />
			<input style="margin-left: 300px;margin-top: 12px;" type="submit" value="开始递归"/>
		</form>
		<br />
		<br />
		<h3>注：输入本程序文件的相对目标文件夹</h3>
		<br />
		<br />
	</body>
</html>